package mySQL;
import java.sql.*;


public class mySQL 
{
	private String url = "jdbc:mysql://db4free.net:3306/";
	private String dbName = "dalgaard";
	private String driver = "com.mysql.jdbc.Driver";
	private String userName = "redlaz"; 
	private String password = "kodetilsql";
	private Connection connection = null;
	private Statement statement = null;
	private ResultSet mySQLoutput = null;
	
	public mySQL()
	{
		try 
		{
			Class.forName(driver).newInstance();
		} 
		
		catch (InstantiationException e1) 
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
		
		catch (IllegalAccessException e1) 
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
		
		catch (ClassNotFoundException e1) 
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	
	public void add()
	{
		if (connect())
		{
			try 
			{
				statement = connection.createStatement();
				mySQLoutput = statement.executeQuery("SELECT * FROM test");
				
				while (mySQLoutput.next())
				{
					System.out.println(mySQLoutput.getString("domain") + ", " + mySQLoutput.getString("userAgent"));
				}
			} 
			
			catch (SQLException e) 
			{
				System.out.println(e.getMessage());
			}
		}
		closeConnection();
	}
	
	private boolean connect()
	{
		try 
		{
			connection = DriverManager.getConnection(url+dbName,userName,password);
			System.out.println("Connected to mySql database");
			return true;
		} 
		
		catch (SQLException e1) 
		{
			System.out.println(e1.getMessage());
			return false;
		}
	}
	
	private boolean closeConnection()
	{
		try 
		{
			if (connection != null)
			{
				if (!connection.isClosed())
				{
					connection.close();
					System.out.println("Disconnected from mySql database");
					return true;
				}
			}
		} 
		 
		 catch (Exception e) 
		 {
			 System.out.println(e.getMessage());
		 }
		return false;
	}
}
